package com.ny.controller;
import com.ny.DeviceRepository;
import com.ny.entity.Device;
import com.ny.entity.Result;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    
    @Autowired
    private DeviceRepository deviceRepository;

    // 接口：http://localhost:8081/findAll
    @GetMapping("/findAll")
    public Result findAll(){
        return Result.success(deviceService.findAll());
    }
    
    // 添加设备接口：http://localhost:8081/addDevice
    @PostMapping("/addDevice")
    public Result addDevice(@RequestBody Device device){
        Device savedDevice = deviceRepository.save(device);
        return Result.success(savedDevice);
    }
    
    // 也允许通过GET方法添加测试设备
    @GetMapping("/addDevice")
    public Result addDeviceGet() {
        Device device = new Device();
        device.setDeviceId(1);
        device.setDeviceName("测试设备");
        device.setDeviceType(1);
        device.setStatus(1);
        device.setRoomId(1);
        
        Device savedDevice = deviceRepository.save(device);
        return Result.success(savedDevice);
    }
}